import java.util.ArrayList;
import java.util.List;

import tree_edit_dist.TreeEditDist;
import xmlpreprocessed.Node;
import xmlpreprocessed.EntailmentCorpus;
import xmlpreprocessed.Pair;
import xmlpreprocessed.Sentence;
import xmlpreprocessed.XMLParser;

/**
 * Created by IntelliJ IDEA.
 * User: oye
 * Date: 14.11.11
 * Time: 15:46
 * To change this template use File | Settings | File Templates.
 */
public class RTEPhase2 {
	
	
	XMLParser parser = new XMLParser();
	EntailmentCorpus entailmentCorpus = parser.getEntailementCorpus("rte2_dev_data/RTE2_dev.preprocessed.xml");
	List<Pair> pairs = entailmentCorpus.getProperties();

	public tree_edit_dist.Node buildTrees(double treshold) {
        TreeEditDist treeEditDist = new TreeEditDist();

		for (xmlpreprocessed.Pair pair : pairs) {
		    List<Node> nodesInT = getNodesFromSentence(pair.getText().getProperties());
            List<Node> nodesInH = getNodesFromSentence(pair.getHypothesis().getProperties());

            List<tree_edit_dist.Node> tTreeRoots = buildTree(nodesInT);
            List<tree_edit_dist.Node> hTreeRoots = buildTree(nodesInH);

            int sum=0;
            for (tree_edit_dist.Node hTreeRoot : hTreeRoots) {
                int hSentencedist=0;
                for (tree_edit_dist.Node tTreeRoot : tTreeRoots) {
                    hSentencedist += treeEditDist.distance(hTreeRoot, tTreeRoot);
                }
                sum = hSentencedist/tTreeRoots.size();
            }
            sum = sum/hTreeRoots.size();

            System.out.println("Distance: "+sum);
        }
		return null;
	}

        private List<Node> getNodesFromSentence(List<Sentence> sentences) {
            List<Node> nodesInT = new ArrayList<Node>();
            for (Sentence sentence : sentences) {
                nodesInT.addAll(sentence.getProperties());
            }
            return nodesInT;
        }
	
	public List<tree_edit_dist.Node> buildTree(List<Node> nodes) {
	    List<tree_edit_dist.Node> treeRoots = new ArrayList<tree_edit_dist.Node>();

		List<tree_edit_dist.Node> treeNodes = new ArrayList<tree_edit_dist.Node>();
		tree_edit_dist.Node treeNode;
		for (Node node : nodes) {
			if (node.getRelation() == null) {
				treeNode = new tree_edit_dist.Node(node.getLemma(), node.getId(), "");
			} else {
				treeNode = new tree_edit_dist.Node(node.getLemma(), node.getId(), node.getRelation().getParent());
			}
			treeNodes.add(treeNode);
		}
		String parentId ="";
		for (Node node : nodes) {
			if (node.getRelation() != null) {
				parentId = node.getRelation().getParent();
						getParentNode(parentId, treeNodes).add(treeNodes.get(nodes.indexOf(node)));	
			}
		}
		for (tree_edit_dist.Node node : treeNodes) {
			if (node.label.equals("fin") && node.getParent().equals("") && node.getLeftChild()!= null && !node.getLeftChild().label.equals(".") )
				treeRoots.add(node);
			
		}
		return treeRoots;
	}

	private tree_edit_dist.Node getParentNode(String parentId, List<tree_edit_dist.Node> treeNodes) {
		for (tree_edit_dist.Node treeNode : treeNodes) {
			if (treeNode.getId().equals(parentId.trim())) {
				return treeNode; //parent found
			}
		}
		return null;
	}

    public static void main(String[] args) {
        RTEPhase2 test = new RTEPhase2();
        test.buildTrees(0.5);
    }

}
